package com.like.adapter;

import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.like.entity.InfoGetReplyResp;
import com.like.entity.M;
import com.like.hospitalworld.R;
import com.like.likeutils.util.ListViewUtil;
import com.like.utils.DateConverter;
import com.like.utils.ImageUtils;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ArticleReplyAdapter extends BaseAdapter {

    public static final int TYPE_TITLE = 0;
    public static final int TYPE_CONTENT = 1;

    private SparseArray<MoreReplyAdapter> mMoreAdapters;
    private List<InfoGetReplyResp.InformationComment> mAllComments;
    private List<InfoGetReplyResp.InformationComment> mComments;
    private M m;

    public ArticleReplyAdapter(List<InfoGetReplyResp.InformationComment> comments, M m) {
        this.mAllComments = comments;
        this.mMoreAdapters = new SparseArray<>();
        this.mComments = handleReplies(mAllComments);
        this.m = m;
    }

    public void update(List<InfoGetReplyResp.InformationComment> comments, boolean append) {
        this.mMoreAdapters.clear();
        if (append) {
            this.mAllComments.addAll(comments);
            this.mComments = handleReplies(mAllComments);
        } else {
            this.mAllComments = comments;
            this.mComments = handleReplies(mAllComments);
        }
        notifyDataSetChanged();
    }

    private List<InfoGetReplyResp.InformationComment> handleReplies(List<InfoGetReplyResp.InformationComment> comments) {
        List<InfoGetReplyResp.InformationComment> result = new ArrayList<>();
        List<InfoGetReplyResp.InformationComment> subComments = new ArrayList<>();
        Map<Integer, InfoGetReplyResp.InformationComment> rootComments = new HashMap<>();
        for (InfoGetReplyResp.InformationComment comment : comments) {
            if (comment.subComments == null)
                comment.subComments = new ArrayList<>();
            if (comment.subId <= 0)
                rootComments.put(comment.id, comment);
            else
                subComments.add(comment);
        }
        for (InfoGetReplyResp.InformationComment comment : subComments) {
            InfoGetReplyResp.InformationComment parentComment = rootComments.get(comment.subId);
            if (parentComment != null)
                parentComment.subComments.add(comment);
        }
        for (int key : rootComments.keySet())
            result.add(rootComments.get(key));
        return result;
    }

    @Override
    public int getCount() {
        return mComments.size();
    }

    @Override
    public InfoGetReplyResp.InformationComment getItem(int position) {
        return mComments.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
//        if (position == 0 || position == 4)
//            return 0;
        return 1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TitleViewHolder tvh = null;
        ContentViewHolder cvh = null;
        int type = getItemViewType(position);
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        if (convertView == null) {
            switch (type) {
                case TYPE_TITLE:
                    convertView = inflater.inflate(R.layout.item_article_reply_title, parent, false);
                    tvh = new TitleViewHolder(convertView);
                    convertView.setTag(tvh);
                    break;
                case TYPE_CONTENT:
                    convertView = inflater.inflate(R.layout.item_article_reply_content, parent, false);
                    cvh = new ContentViewHolder(convertView);
                    convertView.setTag(cvh);
                    break;
            }
        } else {
            switch (type) {
                case TYPE_TITLE:
                    tvh = (TitleViewHolder) convertView.getTag();
                    break;
                case TYPE_CONTENT:
                    cvh = (ContentViewHolder) convertView.getTag();
                    break;
            }
        }
        switch (type) {
            case TYPE_TITLE:
                break;
            case TYPE_CONTENT:
                InfoGetReplyResp.InformationComment comment = getItem(position);
                cvh.setTags(position);
                MoreReplyAdapter adapter = null;
                if (comment.subComments.size() <= 3)
                    cvh.sExpandMore.setVisibility(View.GONE);
                else
                    cvh.sExpandMore.setVisibility(View.VISIBLE);
                if (mMoreAdapters.get(position) == null) {
                    adapter = new MoreReplyAdapter(comment.subComments);
                    mMoreAdapters.put(position, adapter);
                }
                cvh.sUsername.setText(comment.ickname);
                cvh.sTime.setText(DateConverter.convertShitToDate(comment.commentTime, "yyyy/MM/dd"));
                cvh.sReplyContent.setText(comment.commentText);
                cvh.sLikeCount.setText(comment.likeAmount + "");
                cvh.sReplyCount.setText(comment.subAmount + "");
                ImageUtils.showImage(m.mContext, comment.head, m.getToken(), cvh.sUserIcon);
                adapter = mMoreAdapters.get(position);
                cvh.sReplyList.setAdapter(adapter);
                ListViewUtil.setListViewHeightBasedOnChildren(cvh.sReplyList, false);
                if (adapter.isExpand) {
                    Glide.with(m.mContext.getApplicationContext()).load(R.drawable.reply_up).into(cvh.sExpandIcon);
                    cvh.sExpandtext.setText("收起更多回复");
                } else {
                    Glide.with(m.mContext.getApplicationContext()).load(R.drawable.reply_more).into(cvh.sExpandIcon);
                    cvh.sExpandtext.setText("展开更多回复");
                }
                break;
        }
        return convertView;
    }

    public class TitleViewHolder {

        public TitleViewHolder(View convertView) {
            x.view().inject(this, convertView);
        }

    }

    public class ContentViewHolder {

        @ViewInject(R.id.expand_text)
        TextView sExpandtext;
        @ViewInject(R.id.expand_icon)
        ImageView sExpandIcon;
        @ViewInject(R.id.reply_container)
        ViewGroup sReplyContainer;
        @ViewInject(R.id.reply_list)
        ListView sReplyList;
        @ViewInject(R.id.expand_more)
        ViewGroup sExpandMore;
        @ViewInject(R.id.user_icon)
        ImageView sUserIcon;
        @ViewInject(R.id.username)
        TextView sUsername;
        @ViewInject(R.id.reply_content)
        TextView sReplyContent;
        @ViewInject(R.id.time)
        TextView sTime;
        @ViewInject(R.id.like_count)
        TextView sLikeCount;
        @ViewInject(R.id.reply_count)
        TextView sReplyCount;
        @ViewInject(R.id.like_container)
        ViewGroup sLikeContainer;

        public void setTags(int position) {
            sExpandMore.setTag(position);
            sLikeContainer.setTag(position);
        }

        @Event(R.id.expand_more)
        private void expandClick(View v) {
            int position = (int) v.getTag();
            MoreReplyAdapter adapter = mMoreAdapters.get(position);
            if (adapter.isExpand)
                adapter.collapse();
            else
                adapter.expand();
            ListViewUtil.setListViewHeightBasedOnChildren(sReplyList, false);
            notifyDataSetChanged();
        }

        @Event(R.id.like_container)
        private void likeClick(View v) {

        }

        public ContentViewHolder(View convertView) {
            x.view().inject(this, convertView);
        }

    }
}
